package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.client.dto.AccountDto;
import com.woniu.client.dto.JunkTypeDto;
import com.woniu.client.dto.OrderDetailsDto;
import com.woniu.client.dto.OrderDto;
import com.woniu.controller.form.*;
import com.woniu.model.Result;
import com.woniu.model.Station;
import com.woniu.service.StationService;
import com.woniu.service.param.*;
import com.woniu.comment.util.FileUtil;
import com.woniu.comment.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/station")
@Api(tags = "站点相关接口")
public class StationController {
    @Resource
    private StationService stationService;

    /**
     *新增站点
     */
    @PostMapping("/add")
    @ApiOperation("新增站点")
    public Result<?> add(@RequestBody AddStationFrom form) throws Exception {


        AddStationParam param = BeanUtil.toBean(form, AddStationParam.class);

        stationService.add(param);
        return Result.newInstance(null).setCode(200).setData(null).setMessage("新增站点成功!");
    }

    /**
     *删除站点
     */
    @GetMapping("/del/{id}")
    @ApiOperation("删除站点")
    public Result<?> del(@PathVariable("id") Integer id) throws Exception {
        stationService.del(id);
        return Result.newInstance(null).setCode(200).setData(null).setMessage("删除站点成功!");
    }

    /**
     *删除站点
     */
    @GetMapping("/changeState/{id}")
    @ApiOperation("删除站点")
    public Result<?> changeState(@PathVariable("id") Integer id) throws Exception {
        stationService.changeState(id);
        return Result.newInstance(null).setCode(200).setData(null).setMessage("修改站点状态成功!");
    }

    /**
     *修改站点图片
     */
    @PostMapping("/modify")
    @ApiOperation("修改站点图片")
    public Result<?> modify(ModifyStationForm form) throws Exception {
        ModifyStationParam param = BeanUtil.toBean(form, ModifyStationParam.class);
        // @RequestBody不能加，因为本次请求是传了文件的，不加该注解;文件只能用表单参数
        if (!FileUtil.isImageFile(form.getCard().getInputStream())) {
            throw new Exception("文件类型有误，请检查");
        }
        //判断是否增加了新的图片
        String oldPictorFilename = form.getOldImg();
        String oldname = form.getCard().getOriginalFilename();
        if (!Objects.equals(oldPictorFilename, oldname)) {
            param.setUpNew(true);
        }
        String substring = oldname.substring(oldname.lastIndexOf("."));
        String filename = UUID.randomUUID().toString();
        String filePath = OSSUtil.uploadFile(form.getCard().getInputStream(), filename + substring, "jinaisen");
        param.setCard(filePath);

        stationService.modify(param);

        return Result.newInstance( null).setMessage("修改站点信息成功!").setCode(200);
    }

    /**
     *修改站点信息(不含图片)
     */
    @PostMapping("/modify2")
    @ApiOperation("修改站点信息(不含图片)")
    public Result modify2(@RequestBody ModifyStation2Form mdf) {
        stationService.modify2(BeanUtil.toBean(mdf, ModifyStation2Param.class));
        return Result.newInstance( null).setMessage("修改站点信息成功!").setCode(200);
    }

    /**
     * 根据经纬度查询最近站点
     * form :
     * longitude 经度
     * latitude 纬度
     */
    @PostMapping("/findByLocation")
    @ApiOperation("根据经纬度查询最近站点")
    public Result<Station> findByLocation(@RequestBody FindByLocationForm flf) {
        Station station = stationService.findByLocation(BeanUtil.toBean(flf, FindByLocationParam.class));
        if (station==null){
            throw new RuntimeException("附近五公里没有最近站点");
        }
        return Result.newInstance( Station.class).setMessage("修改站点信息成功!").setCode(200).setData(station);
    }

    /**
     * 根据经纬度和废品种类查询最近站点
     * form :
     * id为废品种类id(只能是一级类别)
     * longitude 经度
     * latitude 纬度
     */
    @PostMapping("/findByLocationAndJunk")
    @ApiOperation("根据经纬度和废品种类查询最近站点")
    public Result<Station> findByLocationAndJunk(@RequestBody FindByLocationAndJunkForm form) {
        Station station = stationService.findByLocationAndJunk(BeanUtil.toBean(form, FindByLocationAndJunkParam.class));
        if (station==null){
            throw new RuntimeException("抱歉,您所在的位置暂不提供服务");
        }
        return Result.newInstance(Station.class).setMessage("根据经纬度和废品种类查询最近站点成功!").setCode(200).setData(station);
    }



    /**
     * 根据站点id查询站点
     * id 站点id
     */
    @GetMapping("/byId/{id}")
    @ApiOperation("根据站点id查找站点")
    public Result<Station> byId(@PathVariable("id") Integer id) throws Exception {
        Station station = stationService.getById(id);
        return Result.newInstance(Station.class).setCode(200).setData(station).setMessage("根据站点id查找站点成功!");
    }

    /**
     * 根据站点id查询站长对象
     * id 站点id
     */
    @GetMapping("/findBossByStationId/{id}")
    @ApiOperation("根据站点id查找站长")
    public Result<Integer> findBossByStationId(@PathVariable("id") Integer id) throws Exception {
        Integer id1 = stationService.findBossByStationId(id);
        return Result.newInstance(Integer.class).setCode(200).setData(id1).setMessage("根据站点id查找站长成功!");
    }

    /**
     * 根据站点id查询站点待审核订单
     * id 站点id
     */
    @GetMapping("/findUnHandelOrderByStationId/{id}")
    @ApiOperation("根据站点id查询站点待审核订单")
    public Result<Page<OrderDto>> findUnHandelOrderByStationId(@PathVariable("id") Integer id) throws Exception {
        Page<OrderDto> page = stationService.findUnHandelOrderByStationId(id);
        return Result.newPageInstance(OrderDto.class).setCode(200).setData(page).setMessage("根据站点id查询站点待审核订单成功!");
    }

    /**
     * 根据站点id查询站点全部订单信息
     * id 站点id
     */
    @GetMapping("/findOrderByStationId")
    @ApiOperation("根据站点id查询站点全部订单信息")
    public Result<Page<OrderDto>> findOrderByStationId(@RequestParam("id") Integer id,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize) throws Exception {
        Page<OrderDto> page = stationService.findOrderByStationId(id,pageNum,pageSize);
        return Result.newPageInstance(OrderDto.class).setCode(200).setData(page).setMessage("根据站点id查询站点全部订单信息成功!");
    }

    /**
     * 加载全部站点Page
     */
    @GetMapping("/loadAll")
    @ApiOperation("根据站点id查找站点")
    public Result<Page<Station>> loadAll(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize)throws Exception {
        Page<Station> station = stationService.loadAll(pageNum,pageSize);
        return Result.newPageInstance(Station.class).setCode(200).setData(station).setMessage("查询所有站点成功!!");
    }


    /**
     * 更改站点负责人
     * id 新负责人id和站点id
     */
    @GetMapping("/changeHandler/{newHandlerId}/{stationId}")
    @ApiOperation("更改站点负责人")
    public Result<?> changeHandler(@PathVariable("newHandlerId") Integer newHandlerId,@PathVariable("stationId") Integer stationId) throws Exception {
        System.out.println("newHandlerId :"+newHandlerId+" stationId: "+stationId);
        stationService.changeHandler(newHandlerId,stationId);
        return Result.newInstance(null).setCode(200).setMessage("更改站点负责人成功!");
    }

}

